library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(viridis)
## Loading required package: viridisLite
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data("nyc_airbnb")
set.seed(1)
data(nyc_airbnb)
nyc_airbnb =
nyc_airbnb %>%
mutate(rating = review_scores_location / 2) %>%
select(
neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) %>%
filter(
!is.na(rating),
neighbourhood_group == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500) %>%
sample_n(5000)
graph of airbnb data: scatterplot If you hover over the different datapoints you get information about each value x axis and points in between (scatter or line), mode= markers, don’t connect dots
nyc_airbnb %>%
mutate(text_label = str_c("Price: $", price, '\nRating: ', rating)) %>%
plot_ly(
x = ~lat, y = ~long, type = "scatter", mode = "markers",
color = ~price, text = ~text_label, alpha = 0.5)
## This version of Shiny is designed to work with 'htmlwidgets' >= 1.5.
## Please upgrade via install.packages('htmlwidgets').
common_neighborhoods =
nyc_airbnb %>%
count(neighbourhood, sort = TRUE) %>%
top_n(8) %>%
select(neighbourhood)
## Selecting by n
inner_join(nyc_airbnb, common_neighborhoods, by = "neighbourhood") %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
plot_ly(y = ~price, color = ~neighbourhood, type = "box",
colors = "Set2")